const db = require('../../db');
// 查询机器人用于占位
exports.getRobotInfo = function getRobotInfo(callback) {
    const sql = "select id,nickname,headimgurl from newuseraccounts where Robot = ?";
    const values = [1];
    db.pool.getConnection(function (err, connection) {
        connection.query({sql: sql, values: values}, function (err, rows) {
            connection.release();
            if (err) {
                console.log(err);
                callback(0);
            } else {
                if (rows.length === 0) {
                    callback(0);
                } else {
                    callback(rows);
                }
            }
        });
    });
};

exports.getUserToBackList = function getUserToBackList(userId, callback) {
    // var sql = "call RemoveUserFromBackList(?)";
    var sql = "delete from black_list where userId = ?";
    var values = [];

    values.push(userId);

    db.pool.getConnection(function (err, connection) {

        connection.query({sql: sql, values: values}, function (err, rows) {
            connection.release();
            if (err) {
                console.log("RemoveUserFromBackList");
                console.log(err);
                callback(0);
            } else {
                console.log(rows);
                callback(1);
            }
        });
        values = [];
    });
};


// 查询用户是否在黑名单中
exports.deleteUserIdToBackList = function deleteUserIdToBackList(idCard,callback) {
    var sql = 'delete from black_list where id_card = ?';
    var values = [idCard];
    db.pool.getConnection(function (err, connection) {
        connection.query({sql: sql, values: values}, function (err, rows) {
            connection.release();
            console.log("剔除黑名单结果：", rows);
            if (err) {
                callback(-1);
                console.log(err);
            } else {
                callback(0);
            }
        });
    });
};

// 通过userId查询出id_card
exports.getUserIdToIdCard = function getUserIdToIdCard(userId, callback) {
    var sql = 'select * from gameaccount.newuseraccounts where id = ?';
    var values = [userId];
    db.pool.getConnection(function (err, connection) {
        connection.query({sql: sql, values: values}, function (err, rows) {
            connection.release();
            // console.log("sql查询结果：", rows);
            if (err) {
                console.log(err);
                callback(-1);
            } else {
                if (rows.length == 0) {
                    callback(0);
                } else {
                    callback(rows);
                }
            }
        });
    });
};